package tom.spring.cloud.gateway.zuul.config;

import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

/**
 * zuul自定义error返回值，方法很多，下面只是其中一种方式<br>
 * 默认错误执行链
 * sendErrorFilter -> forward(/error) -> BasicErrorController -> error() -> getErrorAttributes ->
 *  AbstractErrorController -> errorAttributes.getErrorAttributes
 *  -> ErrorAttributes(DefaultErrorAttributes)由ErrorMvcAutoConfiguration注入，所以自定义
 *  错误实现bean替换默认的DefaultErrorAttributes的bean即可.
 *  也可以自定义ErrorController的实现bean替换BasicErrorController
 * @author ZHUFEIFEI
 */
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
        Map<String, Object> errors = super.getErrorAttributes(webRequest, includeStackTrace);
        //这里不暴露错误信息给客户端,移除错误信息
        errors.remove("exception");
        return errors;
    }
}
